﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using estudent.Models;

namespace estudent.Controllers
{
    public class ProfesorjiSifrantController : Controller
    {
        estudentEnt db = new estudentEnt();
        //
        // GET: /ProfesorjiSifrant/

        public ActionResult Index()
        {
            return View(db.SProfesorjis.OrderBy(p => p.priimekProfesor).ThenBy(p => p.imeProfesor).ToList());
        }

        //
        // GET: /ProfesorjiSifrant/Details/5

        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /ProfesorjiSifrant/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /ProfesorjiSifrant/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create([Bind(Exclude = "sProfesorja, veljavnost")] SProfesorji ustvarjen)
        {
            try
            {
                if (!ModelState.IsValid)
                    return View();

                db.AddToSProfesorjis(ustvarjen);
                db.SaveChanges();

                Prijava nova = new Prijava
                {
                    UpIme = ustvarjen.sProfesor.ToString(),
                    Geslo = "prof",
                    Vloga = "prof"
                };

                db.AddToPrijavas(nova);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("priimekProfesor", "Napaka");
                return View();
            }
        }
        
        //
        // GET: /ProfesorjiSifrant/Edit/5
 
        public ActionResult Edit(int id)
        {
            var uredi = (from p in db.SProfesorjis
                         where p.sProfesor == id
                         select p).First();

            return View(uredi);
        }

        //
        // POST: /ProfesorjiSifrant/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(SProfesorji urejen)
        {
            try
            {
                var original = (from p in db.SProfesorjis
                                where p.sProfesor == urejen.sProfesor
                                select p).First();

                if (!ModelState.IsValid)
                    return View(original);

                db.ApplyCurrentValues(original.EntityKey.EntitySetName, urejen);
                db.SaveChanges();
 
                return RedirectToAction("Index");
            }
            catch
            {
                ModelState.AddModelError("priimekProfesor", "Napaka");
                return View();
            }
        }

        //
        // GET: /ProfesorjiSifrant/Delete/5
 
        public ActionResult Delete(int id)
        {
            try
            {
                var orig = (from p in db.SProfesorjis
                            where p.sProfesor == id
                            select p).First();
                var spremen = (from p in db.SProfesorjis
                               where p.sProfesor == id
                               select p).First();

                if (orig.veljavnost != null)
                {
                    spremen.veljavnost = !orig.veljavnost;
                }
                else
                {
                    spremen.veljavnost = false;
                }

                db.ApplyCurrentValues(orig.EntityKey.EntitySetName, spremen);
                db.SaveChanges();

                return RedirectToAction("Index");
            }
            catch
            {
                return RedirectToAction("Index");
            }
        }

        //
        // POST: /ProfesorjiSifrant/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
